package com.bang.study.esl.model;


import com.bang.study.esl.listener.EslEventListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.freeswitch.esl.client.inbound.Client;
import org.freeswitch.esl.client.internal.IModEslApi;
import org.freeswitch.esl.client.transport.event.EslEvent;
import org.freeswitch.esl.client.transport.message.EslMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;
import java.util.concurrent.CompletableFuture;


@Slf4j
@Component
public class EslClient {
    private static String freeswitchServerIp;

    private static Integer freeswitchServerPort;

    private static String freeswitchServerPassword;


    private static EslEventListener eslEventListener;

    @Value("${freeswitch.server.ip}")
    public void setFreeswitchServerIp(String freeswitchServerIp) {
        EslClient.freeswitchServerIp = freeswitchServerIp;
    }

    @Value("${freeswitch.server.port}")
    public void setFreeswitchServerPort(Integer freeswitchServerPort) {
        EslClient.freeswitchServerPort = freeswitchServerPort;
    }

    @Value("${freeswitch.server.password}")
    public void setFreeswitchServerPassword(String freeswitchServerPassword) {
        EslClient.freeswitchServerPassword = freeswitchServerPassword;
    }

    @Autowired
    public void setEslEventListener(EslEventListener eslEventListener) {
        EslClient.eslEventListener = eslEventListener;
    }

    /**
     * ESL Client
     */
    private static Client eslClient;

    private EslClient() {
    }

    public static synchronized Client getInstance() {
        if (null == eslClient || !eslClient.canSend()) {
            eslClient = new Client();
            connection();
        }
        return eslClient;
    }

    /**
     * 建立连接
     *
     * @return
     */
    private static Client connection() {
        if (StringUtils.isEmpty(freeswitchServerIp) || null == freeswitchServerPort || StringUtils.isEmpty(freeswitchServerPassword)) {
            log.error(" {}:port={}/pwd={}参数错误:", freeswitchServerIp, freeswitchServerPort, freeswitchServerPassword);
            System.exit(-1);
        }
        try {
            eslClient.connect(new InetSocketAddress(freeswitchServerIp, freeswitchServerPort), freeswitchServerPassword, 10);
            log.info("[esl]->连接成功");
        } catch (Exception e) {
            log.error("ESL连接异常:", e);
        }
        return eslClient;
    }

    /**
     * esl 连接状态
     *
     * @return
     */
    public static boolean canSend() {
        if (null != eslClient && eslClient.canSend()) {
            return true;
        }
        return false;
    }

    /**
     * 添加监听
     */
    public static void addEventListener() {
        getInstance();
        eslClient.addEventListener(eslEventListener);
    }

    /**
     * 订阅事件
     */
    public static void setEventSubscriptions() {
        getInstance();
        eslClient.setEventSubscriptions(IModEslApi.EventFormat.PLAIN, "CHANNEL_CREATE CHANNEL_PARK CHANNEL_ANSWER PLAYBACK_STOP DTMF CHANNEL_HANGUP CUSTOM callcenter::info asrmessage");
    }

    public EslMessage sendApiCommand(String command, String arg) {
        return eslClient.sendApiCommand(command, arg);
    }

    public CompletableFuture<EslEvent> sendBackgroundApiCommand(String command, String arg) {
        return eslClient.sendBackgroundApiCommand(command, arg);
    }
}
